package com.xx.springboot.improve.conf;

import com.alibaba.druid.pool.DruidDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.sql.SQLException;

/**
 * author: XiongXin
 * description: Druid的DataSource配置类
 * time: 2017/1/11.
 */
@Configuration
@EnableTransactionManagement
@PropertySource("classpath:config/db.properties")
public class DruidDataSourceConfig implements EnvironmentAware {

    private RelaxedPropertyResolver propertyResolver;

    @Value("${initialSize}")
    private Integer initialSize;
    @Value("${minIdle}")
    private Integer minIdle;
    @Value("${maxActive}")
    private Integer maxActive;
    @Value("${maxWait}")
    private long maxWait;
    @Value("${timeBetweenEvictionRunsMillis}")
    private long timeBetweenEvictionRunsMillis;
    @Value("${minEvictableIdleTimeMillis}")
    private long minEvictableIdleTimeMillis;
    @Value("${validationQuery}")
    private String validationQuery;
    @Value("${testWhileIdle}")
    private Boolean testWhileIdle;
    @Value("${testOnBorrow}")
    private Boolean testOnBorrow;
    @Value("${testOnReturn}")
    private Boolean testOnReturn;
    @Value("${poolPreparedStatements}")
    private Boolean poolPreparedStatements;
    @Value("${maxPoolPreparedStatementPerConnectionSize}")
    private Integer maxPoolPreparedStatementPerConnectionSize;
    @Value("${filters}")
    private String filters;
    @Value("${connectionProperties}")
    private String connectionProperties;

    private static Logger logger = LoggerFactory.getLogger(DruidDataSourceConfig.class);

    public void setEnvironment(Environment env){
        this.propertyResolver = new RelaxedPropertyResolver(env, "spring.datasource.");
    }

    @Bean
    public DataSource dataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(propertyResolver.getProperty("url"));
        dataSource.setDriverClassName(propertyResolver.getProperty("driver-class-name"));
        dataSource.setUsername(propertyResolver.getProperty("username"));
        dataSource.setPassword(propertyResolver.getProperty("password"));
        dataSource.setInitialSize(initialSize);
        dataSource.setMinIdle(minIdle);
        dataSource.setMaxWait(maxWait);
        dataSource.setMaxActive(maxActive);
        dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
        dataSource.setPoolPreparedStatements(poolPreparedStatements);
        dataSource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
        dataSource.setTestWhileIdle(testWhileIdle);
        dataSource.setValidationQuery(validationQuery);
        dataSource.setTestOnBorrow(testOnBorrow);
        dataSource.setTestOnReturn(testOnReturn);
        try{
            dataSource.setFilters("stat,wall");
        } catch (SQLException e){
            logger.error("dataSource设置过滤器失败", e);
        }
        logger.info("dataSource设置完成");
        return dataSource;
    }

}
